
#ifndef __MAG_ENCODER_H__
#define __MAG_ENCODER_H__




#include <stdint.h>
#include "main.h"
#include "gpio.h"








#define SPI_CS_ENABLE  HAL_GPIO_WritePin(SENSER_SPI_CS_GPIO_Port, SENSER_SPI_CS_Pin, GPIO_PIN_RESET)
#define SPI_CS_DISABLE HAL_GPIO_WritePin(SENSER_SPI_CS_GPIO_Port, SENSER_SPI_CS_Pin, GPIO_PIN_SET)


/********************************TLE5012B磁编码器寄存器******************************/
// Error masks for safety words
#define SYSTEM_ERROR_MASK           0x4000
#define INTERFACE_ERROR_MASK        0x2000
#define INV_ANGLE_ERROR_MASK        0x1000

// Commands for read
#define READ_STA_CMD_NOSAFETY       0x8000
#define READ_STA_CMD                0x8001
#define READ_ACTIV_STA_CMD          0x8011
#define READ_ANGLE_VAL_CMD          0x8021
#define READ_ANGLE_SPD_CMD          0x8031
#define READ_ANGLE_REV_CMD          0x8041
#define READ_TEMP_CMD               0x8051
#define READ_INTMODE_1              0x8061
#define READ_SIL                    0x8071
#define READ_INTMODE_2              0x8081
#define READ_INTMODE_3              0x8091
#define READ_OFFSET_X               0x80A1
#define READ_OFFSET_Y               0x80B1
#define READ_SYNCH                  0x80C1
#define READ_IFAB                   0x80D1
#define READ_INTMODE_4              0x80E1
#define READ_TEMP_COEFF             0x80F1
#define READ_RAW_X_CMD              0x8101
#define READ_RAW_Y_CMD              0x8111

// Commands for updated read
#define READ_UPD_STA_CMD            0x8401
#define READ_UPD_ANGLE_VAL_CMD      0x8421
#define READ_UPD_ANGLE_SPD_CMD      0x8431
#define READ_UPD_ANGLE_REV_CMD      0x8441

#define READ_BLOCK_CRC              0x8088

// Commands for write
#define WRITE_ACTIV_STA             0x0011
#define WRITE_INTMODE_1             0x5061
#define WRITE_SIL                   0x5071
#define WRITE_INTMODE_2             0x5081
#define WRITE_INTMODE_3             0x5091
#define WRITE_OFFSET_X              0x50A1
#define WRITE_OFFSET_Y              0x50B1
#define WRITE_SYNCH                 0x50C1
#define WRITE_IFAB                  0x50D1
#define WRITE_INTMODE_4             0x50E1
#define WRITE_TEMP_COEFF            0x50F1




/********************************AS5047P磁编码器寄存器******************************/
//spi command frame 格式
#define AS5047_COMMAND_READ 0x4000

//Volatil Registers 的 addrees mapping
#define	NOP_AS5047P_VOL_REG_ADD         0x0000
#define ERRFL_AS5047P_VOL_REG_ADD       0x0001
#define PROG_AS5047P_VOL_REG_ADD        0x0003
#define DIAAGC_AS5047P_VOL_REG_ADD      0x3ffc
#define MAG_AS5047P_VOL_REG_ADD         0x3ffd
#define ANGLEUNC_AS5047P_VOL_REG_ADD    0x3ffe
#define ANGLECOM_AS5047P_VOL_REG_ADD    0x3fff

//non-volatile-registers 的 addrees mapping
#define ZPOSM_AS5047P_nVOL_REG_ADD      0x0016
#define ZPOSL_AS5047P_nVOL_REG_ADD      0x0017
#define SETTINGS1_AS5047P_nVOL_REG_ADD  0x0018
#define SETTINGS2_AS5047P_nVOL_REG_ADD  0x0019
#define RED_AS5047P_VOL_nREG_ADD        0x001a







void mag_encoder_read_start(void);

void mag_encoder_update(void);

void mag_encoder_init(void);





#endif


